import requests
import json
from bs4 import BeautifulSoup
import re
url='https://www.ifeng.com/'
headers = {
            "connection": "close",
            "Cookie": "JSESSIONID=E4674C29E2A76CB08BB651053D8C951E.bswa3n; wipo-visitor-uunid=ff51e08378c28600; "
                      "_gcl_au=1.1.661799052.1650246701; _ga=GA1.3.1709262595.1650246701; "
                      "_pk_ref.14.ec75=%5B%22%22%2C%22%22%2C1650442707%2C%22https%3A%2F%2Fwww3.wipo.int%2F%22%5D; "
                      "_hjSessionUser_787562=eyJpZCI6IjhhNGViODJkLTFiNTEtNWNmNC1iMDc0LTliNDRiZGJkYTlhZCIsImNyZWF0ZWQ"
                      "iOjE2NTA0NDI3MDc5NTAsImV4aXN0aW5nIjpmYWxzZX0=; "
                      "_pk_id.14.ec75=845d6b854d46c8ec.1650440759.2.16504 "
                      "42818.1650440759.; _gid=GA1.3.807169207.1650806717; _gid=GA1.2.807169207.1650806717; "
                      "_ga=GA1.1.17092 "
                      "62595.1650246701; _pk_id.9.ec75=3222e84a40150571.1650246702.; "
                      "_pk_id.9.d630=a4ae4c09b954546d.1650246701 "
                      ".; _pk_uid=0%3DczoxNjoiMzIyMmU4NGE0MDE1MDU3MSI7%3A_%3D4d811534abc282543fa0eeaad6da945e10b9c701"
                      "; _ga_15TSHJ0H "
                      "WP=GS1.1.1651022240.33.0.1651022878.0",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/95.0.4638.54 Safari/537.36",
        }
res =requests.get(url,headers=headers)
soup=BeautifulSoup(res.text,'lxml')
a=soup.find_all('script')
b=a[3].text
b=b.replace('\n','')
# print(b)


#标题
part_title=re.compile('"title":"(.*?)"',re.S)
title=re.findall(part_title,str(b))
# # print(title)
# # print(len((title))) #1189
index=title.index('《梦中的那片海》杀青 李沁肖战手捧鲜花告别剧组')
# # print(index)
title_list=title[58:1158]
# # print(title_list)

#网址
part_url=re.compile('"url":"(.*?)"',re.S)
new_url=re.findall(part_url,str(b))[58:1158]
# print(new_url)
# print(len(new_url))
for m in range(len(new_url)):
    with open('C:\\Users\\DELL\\Desktop\\ifeng.csv', 'a', encoding='utf8') as f:
        f.write('{},{}\n'.format(title_list[m], new_url[m]))
        f.close()